package com.chenfeng.investor.service.impl;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.chenfeng.common.util.PageInfoUtil;
import com.chenfeng.investor.dao.IABankDao;
import com.chenfeng.investor.dao.IInvestorDao;
import com.chenfeng.investor.pojo.ABank;
import com.chenfeng.investor.pojo.AInvestor;
import com.chenfeng.investor.service.IinvestorService;

@Service("investorService")
public class InvestorServiceImpl implements IinvestorService
{

	@Resource
	private IABankDao bankDao;
	@Resource
	private IInvestorDao investorDao;
	/**
	 * 插入一条银行记录
	 */
	@Override
	public JSONObject insertOneBank(ABank bank)
	{
		JSONObject resuJson = new JSONObject();
		//注释部分为测试事物回滚是否是正常的
		/*String str =null;
		str.toString();*/
		
		//返回值为影响的行数
		int majorKey = this.bankDao.insert(bank);
		if(majorKey>0)
		{
			resuJson.put("code", "0");
			resuJson.put("info", "成功");
			//构建二级json 将主键存储在json对象中
			JSONObject date = new JSONObject();
			date.put("主键", bank.getId());
			resuJson.put("date", date);
		}else
		{
			resuJson.put("code", "1");
			resuJson.put("info", "失败");
		}
		return resuJson;
	}
	/**
	 * 查找一条银行记录
	 */
	@Override
	public ABank findOneBank(Map<String, Object> map)
	{
		return this.bankDao.findOne(map);
	}
	/**
	 * mapper中的sql语句本质上还是分页与不分页（分页的情况下要进行两步走 第一总记录数 第二记录数）
	 * 查找多条银行记录
	 */
	@Override
	public List<ABank> findCondListBank(PageInfoUtil pageInfoUtil, Map<String, Object> map)
	{
		List<ABank> bankList = Collections.EMPTY_LIST;
		//分页
		//true跟false在一一起判断
		if(map.get("keyword") != null)
		{
			map.put("keyword", "%"+map.get("keyword")+"%");
		}
		if(pageInfoUtil != null)
		{
			//获取总记录数
			map.put("pageCount", "true");
			bankList =  this.bankDao.findCondList(map);
			pageInfoUtil.setTotalRecord(bankList.get(0).getId());
			//开始分页
			map.put("pageCount", "false");
			map.put("currentRecord", pageInfoUtil.getCurrRecord());
			map.put("pageSize",pageInfoUtil.getPageSize());
			bankList =  this.bankDao.findCondList(map);
		}else
		//不分页	
		{
			bankList = this.bankDao.findCondList(map);
		}
		return bankList;
	}
	@Override
	public JSONObject updateOneBank(ABank bank)
	{
		JSONObject resuJson = new JSONObject();	
		//返回值为影响的行数
		int majorKey = this.bankDao.update(bank);
		if(majorKey>0)
		{
			resuJson.put("code", "0");
			resuJson.put("info", "更新成功");
		}else
		{
			resuJson.put("code", "1");
			resuJson.put("info", "更新失败");
		}
		return resuJson;
	}
	@Override
	public JSONObject deleteOneBank(Map<String,Object> map)
	{
		JSONObject resuJson = new JSONObject();	
		//返回值为影响的行数
		int majorKey = this.bankDao.delete(map);
		if(majorKey>0)
		{
			resuJson.put("code", "0");
			resuJson.put("info", "删除成功");
		}else
		{
			resuJson.put("code", "1");
			resuJson.put("info", "删除失败");
		}
		return resuJson;
	}
	//==================================================================================
	@Override
	public JSONObject insertOneInvestor(AInvestor investor)
	{
		JSONObject resuJson = new JSONObject();
		//注释部分为测试事物回滚是否是正常的
		/*String str =null;
		str.toString();*/
		
		//返回值为影响的行数
		int majorKey = this.investorDao.insert(investor);
		if(majorKey>0)
		{
			resuJson.put("code", "0");
			resuJson.put("info", "成功");
			//构建二级json 将主键存储在json对象中
			JSONObject date = new JSONObject();
			date.put("主键", investor.getId());
			resuJson.put("date", date);
		}else
		{
			resuJson.put("code", "1");
			resuJson.put("info", "失败");
		}
		return resuJson;
	}
	/**
	 * 查找一条银行记录
	 */
	@Override
	public AInvestor findOneInvestor(Map<String, Object> map)
	{
		return this.investorDao.findOne(map);
	}
	/**
	 * mapper中的sql语句本质上还是分页与不分页（分页的情况下要进行两步走 第一总记录数 第二记录数）
	 * 查找多条银行记录
	 */
	@Override
	public List<AInvestor> findCondListInvestor(PageInfoUtil pageInfoUtil, Map<String, Object> map)
	{
		List<AInvestor> investorList = Collections.EMPTY_LIST;
		//分页
		//true跟false在一一起判断
		if(map.get("keyword") != null)
		{
			map.put("keyword", "%"+map.get("keyword")+"%");
		}
		if(pageInfoUtil != null)
		{
			//获取总记录数
			map.put("pageCount", "true");
			investorList =  this.investorDao.findCondList(map);
			pageInfoUtil.setTotalRecord(investorList.get(0).getId());
			//开始分页
			map.put("pageCount", "false");
			map.put("currentRecord", pageInfoUtil.getCurrRecord());
			map.put("pageSize",pageInfoUtil.getPageSize());
			investorList =  this.investorDao.findCondList(map);
		}else
		//不分页	
		{
			investorList = this.investorDao.findCondList(map);
		}
		return investorList;
	}
	@Override
	public JSONObject updateOneInvestor(AInvestor investor)
	{
		JSONObject resuJson = new JSONObject();	
		//返回值为影响的行数
		int majorKey = this.investorDao.update(investor);
		if(majorKey>0)
		{
			resuJson.put("code", "0");
			resuJson.put("info", "更新成功");
		}else
		{
			resuJson.put("code", "1");
			resuJson.put("info", "更新失败");
		}
		return resuJson;
	}
	@Override
	public JSONObject deleteOneInvestor(Map<String,Object> map)
	{
		JSONObject resuJson = new JSONObject();	
		//返回值为影响的行数
		int majorKey = this.investorDao.delete(map);
		if(majorKey>0)
		{
			resuJson.put("code", "0");
			resuJson.put("info", "删除成功");
		}else
		{
			resuJson.put("code", "1");
			resuJson.put("info", "删除失败");
		}
		return resuJson;
	}
}
